package com.yanqu.road.server.cmd.activity.oldplayerreturn;

import com.yanqu.road.entity.activity.oldplayerreturn.GetReturnPlayerInfoRes;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.OldPlayerReturnProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.oldplayerreturn.OldPlayerReturnMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

/**
 * @author ：zfr
 * @date ：2021/12/7 16:16
 */
@Cmd(code = GameProtocol.S_CROSS_GET_RETURN_ROLE_INFO, desc = "跨服来获取回归角色信息")
public class CrossGetReturnRoleInfoCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        OldPlayerReturnProto.GetOtherReturnRoleInfoReqToServerMsg msg = OldPlayerReturnProto.GetOtherReturnRoleInfoReqToServerMsg.parseFrom(packet.getBytes());
        GetReturnPlayerInfoRes returnPlayerInfoRes = OldPlayerReturnMgr.getReturnPlayerInfo(msg.getOpenId());
        List<UserInfo> userInfos = returnPlayerInfoRes.getUserInfoList();
        OldPlayerReturnProto.GetOtherReturnRoleInfoRespToCrossMsg.Builder respMsg = OldPlayerReturnProto.GetOtherReturnRoleInfoRespToCrossMsg.newBuilder();
        respMsg.setOriServerId(msg.getOriServerId());
        respMsg.setOriPlayerId(msg.getOriPlayerId());
        for (UserInfo userInfo : userInfos) {
            if (GameServer.getInstance().getServerId() == msg.getOriServerId() && userInfo.getUserId() == msg.getOriPlayerId()) {
                break;
            }
            OldPlayerReturnProto.RoleUserInfoTemp.Builder temp = OldPlayerReturnProto.RoleUserInfoTemp.newBuilder();
            temp.setServerId(userInfo.getOriginalServerId());
            temp.setUserId(userInfo.getUserId());
            temp.setUserInfo(PlayerPb.parsePlayerBaseTempMsg(userInfo));
            respMsg.addUserInfo(temp);
        }
        respMsg.setCanReturnTime(returnPlayerInfoRes.getReturnTime());
        YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_GET_OTHER_RETURN_ROLE_INFO_RESP, respMsg);
        GamePlayerMgr.sendPacket(0, message);
    }
}
